package com.qf.web;


import com.qf.pojo.User;
import com.qf.service.UserService;
import com.qf.service.impl.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@WebServlet("/findUserById")
public class FindUserById extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //接收参数
        String id = req.getParameter("id");
        int i = Integer.parseInt(id);
        //查询数据
        UserService userService = new UserServiceImpl();
        User user  = userService.findUserById(i);
        //跳转到update.jsp完成数据回显
        if (user!=null){
            //将user保存到域对象中
            req.setAttribute("admin",user);
            req.getRequestDispatcher("/update.jsp").forward(req,resp);

        }else {
            resp.sendRedirect("/findAll");
        }
    }
}
